package com.hyh.admin.controller.resources;

import com.hyh.ad.common.core.controller.BaseController;
import com.hyh.ad.common.core.domain.AjaxResult;
import com.hyh.ad.common.utils.SecurityUtils;
import com.hyh.admin.recommend.ScoreCalculatorService;
import com.hyh.admin.recommend.UserProfile;
import com.hyh.admin.recommend.UserProfileService;
import com.hyh.admin.service.IResourcesCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/resources/collect")
@Api(tags = "资源收藏管理")
public class ResourcesCollectController extends BaseController {
    @Autowired
    private IResourcesCollectService resourcesCollectService;

    @Autowired
    private UserProfileService userProfileService;

    @Autowired
    private ScoreCalculatorService scoreCalculatorService;

    /*
     * 查询所有自己的收藏
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有自己的收藏")
    public AjaxResult list() {
        return AjaxResult.success(resourcesCollectService.selectResourcesCollect(SecurityUtils.getLoginUser().getUser().getId()));
    }

    /*
     * 删除收藏
     */
    @DeleteMapping("/delete/{resourceId}")
    @ApiOperation(value = "删除收藏")
    public AjaxResult delete(@PathVariable Long resourceId) {
        return AjaxResult.success(resourcesCollectService.deleteResourcesCollect(SecurityUtils.getLoginUser().getUser().getId(), resourceId));
    }

    /*
     * 新增收藏
     */
    @PostMapping("/insert/{resourceId}")
    @ApiOperation(value = "新增收藏")
    public AjaxResult insert(@PathVariable Long resourceId) {
        //获取用户画像
        UserProfile userProfile = userProfileService.getUserProfile(SecurityUtils.getLoginUser().getUser().getId().toString());
        scoreCalculatorService.updateCourseScore(userProfile,resourceId.toString(),"liked");
        return AjaxResult.success(resourcesCollectService.insertResourcesCollect(SecurityUtils.getLoginUser().getUser().getId(), resourceId));
    }

    /*
     * 批量删除
     */
    @DeleteMapping("/deleteBatch/{ids}")
    @ApiOperation(value = "批量删除")
    public AjaxResult deleteBatch(@PathVariable Long[] ids) {
        return AjaxResult.success(resourcesCollectService.deleteResourcesCollectBatch(SecurityUtils.getLoginUser().getUser().getId(), ids));
    }
}
